package com.example.demo.utils;

import com.example.demo.domain.Goods;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

public class FileUtils
{

    private static final String EXCEL_XLS="xls";
    private static final String EXCEL_XLSX="xlsx";

    /**
     * @Create:Space
     * @Time: 2020-6-10 19:16:00
     * @return 图片上传
     */
    public static String uploadImage(String path, String ip, String port,
                                     MultipartFile file, boolean isRandomName)
    {
        try
        {
            String[] Imgtype={"gif","png","jpg"};
            if(file!=null)
            {
                String origName=file.getOriginalFilename();// 文件原名称
                System.out.println("上传的文件原名称:"+origName);
                // 判断文件类型
                String type=origName.indexOf(".")!=-1?origName.substring(origName.lastIndexOf(".")+1, origName.length()):null;
                if (type!=null)
                {
                    boolean booIsType=false;
                    for (int i = 0; i < Imgtype.length; i++)
                    {
                        if (Imgtype[i].equals(type.toLowerCase()))
                        {
                            booIsType=true;
                        }
                    }
                    //类型正确
                    if (booIsType)
                    {
                        //存放图片文件的路径
                        System.out.print("文件上传的路径"+path);
                        //是否随机名称
                        if(isRandomName)
                        {
                            origName=UUID.randomUUID()+origName.substring(origName.lastIndexOf("."));
                        }
                        System.out.println("随机文件名："+origName);
                        //判断是否存在目录
                        File targetFile=new File(path,origName);
                        if(!targetFile.exists())
                        {
                            targetFile.getParentFile().mkdirs();//创建目录
                        }
                        //上传
                        file.transferTo(targetFile);
                        //完整路径
                        System.out.println("完整路径:"+targetFile.getAbsolutePath());
                        return origName;
                    }
                }
            }
            return null;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }


    /**
     * @Create:     LT4
     * @Time:       2020年6月14日09:20:32
     * @return:     解析excel
     * @Quote：      https://blog.csdn.net/qq_42944520/article/details/84453275?utm_medium=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
     * @Quote：      https://www.jianshu.com/p/38badba5397c
     */
    //-------------------------start-------------------------------
    //获取excel
    public static List<Object> getExcelData(MultipartFile file) throws IOException {
        InputStream in=file.getInputStream();
        //获取workbook
        Workbook workbook=getWorkBook(in,file.getOriginalFilename());
        //默认第一个
        Sheet sheet=workbook.getSheetAt(0);
        return getSheetData(sheet);
    }

    //本地获得Excel
    public static List<Object> getExcelData(String path) throws IOException{
        File file=new File(path);
        InputStream in=new FileInputStream(file);
        //获取workbook
        Workbook workbook=getWorkBook(in,path);
        //默认第一个
        Sheet sheet=workbook.getSheetAt(0);
        return getSheetData(sheet);
    }

    //获取 workbook
    public static Workbook getWorkBook(InputStream in, String path) throws IOException {
        Workbook workbook=null;
        if(path.toLowerCase().endsWith(EXCEL_XLS)){
            workbook=new HSSFWorkbook(in);
        }else if(path.toLowerCase().endsWith(EXCEL_XLSX)){
            workbook=new XSSFWorkbook(in);
        }
        return  workbook;
    }

    //获得 sheet数据
    public static List<Object> getSheetData(Sheet sheet){
        ArrayList<Object> goodsList =new ArrayList<>();
        //获取最大行数
        int n=sheet.getPhysicalNumberOfRows();
        Row row=null;
        //跳过描述
        for(int i=2;i<n;i++){
            row=sheet.getRow(i);
            Goods goods=new Goods();
            goods.setName(row.getCell(0).getStringCellValue());             //名称
            goods.setBuying_price(row.getCell(1).getNumericCellValue());    //进货价
            goods.setSelling_price(row.getCell(2).getNumericCellValue());   //售价
            goods.setType(row.getCell(3).getStringCellValue());             //类型
            goods.setPlace(row.getCell(4).getStringCellValue());            //产地
            goods.setType(row.getCell(5).toString().trim());                //时间
            goods.setState((int) row.getCell(6).getNumericCellValue());     //状态

            goodsList.add(goods);
        }
        return goodsList;
    }
    //--------------------end-------------------------------
}
